package com.kk.controller;

import com.kk.common.Result;
import com.kk.pojo.dto.CaptchaVerifyDTO;
import com.kk.pojo.vo.CaptchaVO;
import com.kk.service.CaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/captcha")
@Slf4j
public class CaptchaController {

    @Autowired
    private  CaptchaService captchaService;

    /**
     * 生成滑动验证码
     *
     * @return 验证码信息
     */
    @GetMapping("/generate")
    public Result<CaptchaVO> generateCaptcha() {
        log.info("开始生成验证码");

        CaptchaVO captchaVO = captchaService.generateCaptcha();
        if (captchaVO == null) {
            return Result.fail("生成验证码失败");
        }

        return Result.success(captchaVO);
    }

    /**
     * 验证滑动验证码
     *
     * @param verifyDTO 验证请求DTO
     * @return 验证结果
     */
    @PostMapping("/verify")
    public Result<Boolean> verifyCaptcha(@Validated @RequestBody CaptchaVerifyDTO verifyDTO) {
        log.info("开始验证验证码, captchaId: {}", verifyDTO.getCaptchaId());

        boolean success = captchaService.verifyCaptcha(verifyDTO);
        if (success) {
            return Result.success(true);
        } else {
            return Result.fail(400, "验证码验证失败");
        }
    }
}